//
//  ReSetPassController1.m
//  FinancialProduct
//
//  Created by Stella on 2017/8/25.
//  Copyright © 2017年 maq. All rights reserved.
//
#import "CaptchaView.h"
#import "ValidateTools.h"
#import "CKPuslichTool.h"
#import "HandleNetLogin.h"
#import "ReSetPassController2.h"
#import "ReSetPassController1.h"

@interface ReSetPassController1 ()
@property (weak, nonatomic) IBOutlet UITextField *userName;
@property (weak, nonatomic) IBOutlet UITextField *phoneNo;
@property (weak, nonatomic) IBOutlet UITextField *myCode;
@property (weak, nonatomic) IBOutlet CaptchaView *codePic;
@property (weak, nonatomic) IBOutlet UIButton *nextButton;
@property (weak, nonatomic) IBOutlet UILabel *reminderLabel1;
@property (weak, nonatomic) IBOutlet UILabel *reminder;
@property (weak, nonatomic) IBOutlet UILabel *reminderLabel2;
@property (weak, nonatomic) IBOutlet UILabel *reminderLabel3;
@end

@implementation ReSetPassController1

- (void)viewDidLoad {
    [super viewDidLoad];
    self.reminderLabel1.text=@"";
    self.reminderLabel2.text=@"";
    self.reminderLabel3.text=@"";
    self.reminder.text=@"";
    self.userName.layer.cornerRadius=5;
    self.phoneNo.layer.cornerRadius=5;
    self.myCode.layer.cornerRadius=5;
    self.nextButton.layer.cornerRadius=5;
    [self setTextfile:self.userName img:[UIImage imageNamed:@"Admin-User"]];
    [self setTextfile:self.phoneNo img:[UIImage imageNamed:@"iPhone-3(G)"]];
    [self MoveTextfileContent:self.myCode];
    [self.phoneNo       addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.userName       addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [self.myCode       addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
- (void)textFieldDidChange:(UITextField *)textField
{
    if (textField==_phoneNo) {
        if (textField.text.length>11) {
            textField.text = [textField.text substringToIndex:11];
        }
    }else if(textField==_myCode){
        if (textField.text.length>20) {
            textField.text = [textField.text substringToIndex:20];
        }
    }else{
        if (textField.text.length>50) {
            textField.text = [textField.text substringToIndex:50];
        }
    }
    
    
}
-(void)MoveTextfileContent:(UITextField *)textfile{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 27)];
    
    imageView.contentMode = UIViewContentModeCenter;
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    textfile.layer.cornerRadius = 5;
}
- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (IBAction)next:(id)sender {
    self.reminderLabel1.text=@"";
    self.reminderLabel2.text=@"";
    self.reminderLabel3.text=@"";
    self.reminder.text=@"";
    //用户姓名
    if (self.userName.text.length==0||[self.userName.text rangeOfString:@"不详"].length>0||[self.userName.text rangeOfString:@"不祥"].length>0||[self.userName.text rangeOfString:@"未知"].length>0||[self.userName.text rangeOfString:@"不知道"].length>0||[self.userName.text rangeOfString:@"先生"].length>0||[self.userName.text rangeOfString:@"小姐"].length>0||self.userName.text.length>50) {
        self.reminderLabel1.text=@"*客户姓名不符合要求";
        return;
    }
    
    //验证手机号码
    if ( self.phoneNo.text.length == 0) {
        self.reminderLabel2.text=@"*请输入手机号";
        return;
    }
    if ( ![ValidateTools validateMobile:self.phoneNo.text]) {
        self.reminderLabel2.text=@"*手机号码格式不正确";
        return;
    }
    
    //验证码
    NSString *text =   [_codePic.changeString lowercaseString];
    if (![text isEqualToString:self.myCode.text]) {
        self.reminderLabel3.text=@"*验证码错误";
        return;
    }
    [HandleNetLogin retrieveUserName:self.userName.text AccountPhone:self.phoneNo.text Success:^(NSDictionary *data) {
        NSDictionary *info=@{@"name":self.userName.text,@"phone":self.phoneNo.text};
        //去第二步
        ReSetPassController2 *vc=(ReSetPassController2 *)[CKPuslichTool getViewControllerFromStroyBoard:@"Login" viewID:@"ReSetPassController2"];
        vc.userinfo=info;
        vc.whereFrom=@"reSetPass";
        [self.navigationController pushViewController:vc animated:YES];
    } Failure:^(NSString *errorInfo) {
        self.reminder.text=[NSString stringWithFormat:@"*%@",errorInfo];
    }];
    
    
    
}

-(void)setTextfile:(UITextField *)textfile img:(UIImage *)img{
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 27)];
    imageView.image =img;
    imageView.contentMode = UIViewContentModeCenter;
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(imageView.frame.size.width-5, 6, 1, textfile.frame.size.height-6*2)];
    view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [textfile addSubview:view];
    
    textfile.leftView = imageView;
    textfile.leftViewMode = UITextFieldViewModeAlways;
    //    textfile.layer.borderColor = [UIColor whiteColor].CGColor;
    textfile.layer.cornerRadius = 5;
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
